﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using SQLHelper;

public partial class admin_User_Show : System.Web.UI.Page
{
    int user_id;

    int[] checkProfileStrIsNullArr = new int[6] { 4, 5, 6, 7, 8, 10 };

    protected void Page_Load(object sender, EventArgs e)
    {
        Master.OnlyAdmin();

        if (Request["id"] != null)
        {
            user_id = Convert.ToInt32(Request["id"]);
            bind();
        }
    }

    protected void Set_Role(object sender, EventArgs e)
    {
        int[] arr = new int[3] { 3, 2, 1 };

        if(ListBox1.SelectedItem.Value != null)
        {
            for(int i = 0; i < arr.Length; i++)
            {
                // 首先检查是否一致，一致返回
                // 不一致就更新并且返回
                if (ListBox1.SelectedItem.Value == arr[i].ToString())
                {
                    string checkSql = "update role_user set role_id = " + arr[i] + " where user_id = " + user_id + "";
                    DB.GetDataNoQuery(checkSql);
                    DB.ColseSqlCon();
                    Response.Redirect("~/admin/User/Show.aspx?id=" + user_id + "");
                }
            }
        }
    }

    protected void Set_State(object sender, EventArgs e)
    {
        if (ListBox2.SelectedItem.Value != null)
        {
            if(ListBox2.SelectedItem.Value == "normal")
            {
                string checkSql = "select * from blacklist where user_id = " + user_id + "";
                SqlDataReader res = DB.GetDataWithQuery(checkSql);
                if (res.Read())
                {
                    DB.ColseSqlCon();
                    // delete
                    string delSql = "delete from blacklist where user_id = " + user_id + "";
                    DB.GetDataNoQuery(delSql);
                    DB.ColseSqlCon();
                }
                Response.Redirect("~/admin/User/Show.aspx?id=" + user_id + "");
            }
            if (ListBox2.SelectedItem.Value == "ban")
            {
                string checkSql = "select * from blacklist where user_id = " + user_id + "";
                SqlDataReader res = DB.GetDataWithQuery(checkSql);
                if (!res.Read())
                {
                    DB.ColseSqlCon();
                    // insert
                    string insSql = "insert into blacklist values(" + user_id + ")";
                    DB.GetDataNoQuery(insSql);
                    DB.ColseSqlCon();
                }
                Response.Redirect("~/admin/User/Show.aspx?id=" + user_id + "");
            }
        }
    }

    void bind()
    {
        string str = "select u.*,r.display_name,b.user_id as ban from users u,roles r,role_user rr left join blacklist b ON rr.user_id = b.user_id where u.id = rr.user_id and r.id = rr.role_id and u.id = " + user_id + ";";
        DataSet res = DB.GetDataWithQuerySecond(str, "users");
        for (int i = 0; i < checkProfileStrIsNullArr.Length; i++)
        {
            if (res.Tables[0].Rows[0][checkProfileStrIsNullArr[i]].ToString() == "")
            {
                if (checkProfileStrIsNullArr[i] == 5)
                {
                    res.Tables[0].Rows[0][checkProfileStrIsNullArr[i]] = "/dist/img/avatar5.png";
                }
                else
                {
                    res.Tables[0].Rows[0][checkProfileStrIsNullArr[i]] = "用户未设置";
                }
            }
        }
        DetailsView1.DataSource = res;
        DetailsView1.DataBind();
        DB.ColseSqlCon();
    }
}